package com.lutzyt.delivery.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lutzyt.delivery.bean.Customer;
import com.lutzyt.delivery.bean.DiscountCoupon;
import com.lutzyt.delivery.bean.DiscountCouponItem;
import com.lutzyt.delivery.bean.Order;
import com.lutzyt.delivery.exception.BusinessException;
import com.lutzyt.delivery.mapper.DiscountCouponItemDAO;
import com.lutzyt.delivery.service.CustomerService;
import com.lutzyt.delivery.service.DiscountCouponItemService;
import com.lutzyt.delivery.service.DiscountCouponService;
import com.lutzyt.delivery.service.OrderService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Random;

/**
 * @author zyt
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class DiscountCouponItemServiceImpl extends ServiceImpl<DiscountCouponItemDAO, DiscountCouponItem> implements DiscountCouponItemService {

  private DiscountCouponItemDAO discountCouponItemDAO;
  private OrderService orderService;
  private CustomerService customerService;
  private DiscountCouponService discountCouponService;

  @Transactional(readOnly = true)
  @Override
  public DiscountCouponItem findById(Integer id) {
    return discountCouponItemDAO.selectById(id);
  }

  @Transactional(readOnly = true)
  @Override
  public IPage<DiscountCouponItem> findByPage(int pageNum, int pageSize, int id) {
    QueryWrapper<DiscountCouponItem> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("discount_coupon_id", id);
    queryWrapper.eq("deleted", 0);
    final IPage<DiscountCouponItem> page = discountCouponItemDAO.selectPage(new Page<>(pageNum, pageSize), queryWrapper);
    for (DiscountCouponItem item : page.getRecords()) {
      Order order = orderService.findById(item.getOrderId());
      final Customer customer = customerService.findById(item.getCustomerId());
      if (order != null){ 
        item.setOrderNumber(order.getNumber());
      }
      item.setCustomerName(customer.getUsername());
    }
    return page;
  }

  @Override
  public void insert(DiscountCouponItem discountCouponItem) {
    discountCouponItemDAO.insert(discountCouponItem);
  }

  @Override
  public void update(DiscountCouponItem discountCouponItem) {
    discountCouponItemDAO.updateById(discountCouponItem);
  }

  @Override
  public void deleteById(Integer id) {
    discountCouponItemDAO.deleteById(id);
  }

  @Override
  public void draw(Integer customerId, Integer discountCouponId) {
    DiscountCouponItem item = this.lambdaQuery()
        .eq(DiscountCouponItem::getDiscountCouponId, discountCouponId)
        .eq(DiscountCouponItem::getCustomerId, customerId)
        .one();
    if (item != null){
      throw new BusinessException(2102,"您已经领取过了，请不要重复领取");
    }
    final DiscountCoupon discountCoupon = discountCouponService.getById(discountCouponId);
    if (discountCoupon.getBalance() <= 0){
      throw new BusinessException(2101,"您来晚了，优惠券已经被抢光了");
    }
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
    Integer count = this.lambdaQuery().eq(DiscountCouponItem::getDiscountCouponId, discountCouponId).count();
    if (count == null){
      count = 0;
    } else {
      count++;
    }
    // 生成一个code yyyyMMddhhmmss01
    String code = sdf.format(discountCoupon.getCreateTime()) + String.format("%0"+ String.valueOf(discountCoupon.getNum()).length() +"d", count);
    item = new DiscountCouponItem();
    item.setDiscountCouponId(discountCouponId);
    item.setCustomerId(customerId);
    item.setCode(code);
    item.setState("UNUSED");
    save(item);
    discountCoupon.setBalance(discountCoupon.getBalance() - 1);
    discountCouponService.update(discountCoupon);
  }

  @Autowired
  public void setDiscountCouponService(DiscountCouponService discountCouponService) {
    this.discountCouponService = discountCouponService;
  }

  @Autowired
  public void setDiscountCouponItemDAO(DiscountCouponItemDAO discountCouponItemDAO) {
    this.discountCouponItemDAO = discountCouponItemDAO;
  }

  @Autowired
  public void setOrderService(OrderService orderService) {
    this.orderService = orderService;
  }

  @Autowired
  public void setCustomerService(CustomerService customerService) {
    this.customerService = customerService;
  }
}